package com.itboye.sunsun.shop.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.CarouselPictureBean;
import com.itboye.sunsun.beans.FiveImageBean;
import com.itboye.sunsun.beans.FiveImageBean.PicturesBean;
import com.itboye.sunsun.beans.NewsHotReaBean;
import com.itboye.sunsun.beans.NewsHotReaBean.AllData.AllList;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.custome.CarouselView;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.luntan.LunTanHomePageActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.news.NewsHotReadMainFragmentActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.web.ui.WebActivity;

public class AllShopActivity extends BaseActivity implements OnClickListener{

	 
	 
	private CarouselView carouselView;
	private ViewGroup banner;
	private ViewGroup shuiZu;
	private ViewGroup chongWu;
	private ViewGroup diaoJuGuan;
	private ViewGroup jingGuanYuanYi;
	private ViewGroup yunDongXiuXian;
	private ViewGroup jiaJuBaiJian;
	private ViewGroup huaHuiYuanYi;
	private ViewGroup huWaiYuanYi;
	
	private View back;
	private ViewGroup chat;
	private ViewGroup shop;
	private AutoTextView announce;//森森热点资讯
	protected String text;//森森热点资讯头条
	protected void onCreate(android.os.Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_all_shop);
		showProgressDialog("数据正在加载中，请稍后",true);
		arrayList_redian=new ArrayList<NewsHotReaBean.AllData.AllList>();
		carouselView=(CarouselView) findViewById(R.id.carouselView);
		addClickListenrForBanner(banner);
 
		getSunsunHotNews();
		getBannerData();
		
		
		getAllGroup();
		
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		chat.setOnClickListener(new OnClickListener() {
					
			@Override
			public void onClick(View v) {
				LoginController.onShopMessageClick(AllShopActivity.this,null);
			}
		});
		shop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LoginController.onShopCarClick(AllShopActivity.this,null);
				
			}
		});
		
		findViewById(R.id.sousuo).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getApplicationContext(), ShopSouSuoActivity.class));
			}
		});
	}
	
	private int sCount=0;
 
    android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
        	
        	if(sCount>=arrayList_redian.size()){
        		sCount=0;
        	}
        	announce.next();
        	announce.setText(arrayList_redian.get(sCount).getPostExcerpt());
        	sCount++;
            handler.sendEmptyMessageDelayed(1, 3000);
        }
    };
	protected ArrayList<AllList> arrayList_redian;
	protected List<PicturesBean> list;


	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler.removeMessages(1);
	}
	private void getSunsunHotNews(){
		MyJsonRequest<NewsHotReaBean.AllData> request = new MyJsonRequest.Builder<NewsHotReaBean.AllData>()
				.apiVer("100")
				.typeKey("BY_News_query")
				.param("position", 6049)
				.param("page_no", 1)
				.requestListener(
						new XRequestListener<NewsHotReaBean.AllData>() {

							@Override
							public void onResponse(NewsHotReaBean.AllData arg0) {
								if (arg0.getList()!=null&&arg0.getList().size()>0) {
									arrayList_redian=arg0.getList();
									handler.sendEmptyMessage(1);
								} 
								
								
							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						text="网络加载失败";
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	private void getBannerData() {
		MyJsonRequest<List<CarouselPictureBean>>request=new MyJsonRequest.Builder<List<CarouselPictureBean>>()
				.apiVer("100")
				.typeKey("BY_Picture_getURL")
				.param("position", "18")
				.requestListener(new XRequestListener<List<CarouselPictureBean>>() {
					
					@Override
					public void onResponse(List<CarouselPictureBean> arg0) {
						List<ImageView>bmps=new ArrayList<ImageView>();
						for (CarouselPictureBean pictureBean : arg0) {
							ImageView imageView=new ImageView(App.ctx);
							
							 imageView.setScaleType(ScaleType.CENTER_CROP);
							 XImageLoader.load(pictureBean.getImgUrl(), imageView);
							bmps.add( imageView);
							imageView.setTag(pictureBean);
							imageView.setOnClickListener(new OnClickListener() {
								
								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									CarouselPictureBean bean=(CarouselPictureBean) v.getTag();
									 //轮播图跳转类型 6070 链接              6071 app商品                             6072 app帖子
									if ((NetPublicConstant.secondUrl+"/Webview/Game/index").equals(bean
											.getUrl())) {
										String uid = SPUtils.get(AllShopActivity.this, null,
												SPContants.USER_ID, "") + "";
										if (uid == null) {
											Intent intent = new Intent(AllShopActivity.this,
													LoginActivity.class);
											startActivity(intent);
										} else {
											Intent intent = new Intent(App.ctx, WebActivity.class);
											intent.putExtra("title", "抽奖");
											intent.putExtra(
													"url",
													bean.getUrl()
															+ "?uid="
															+ uid
															+ "&psw="
															+ SPUtils.get(AllShopActivity.this,
																	null, SPContants.PASSWORD, "") + "");
											startActivity(intent);
										}
										return ;
									}
									if ("6070".equals(bean.getUrlType())) {
										Intent intent=new Intent(getApplicationContext(),WebActivity.class);
//										intent.putExtra("title", "商品详情");
										intent.putExtra("url", bean.getUrl());
										startActivity(intent);
									}else if ("6071".equals(bean.getUrlType())) {
										Intent intent=new Intent(getApplicationContext(), GoodDetailActivity.class);
										intent.putExtra("what", bean.getUrl());
										startActivity(intent);
									}else if ("6072".equals(bean.getUrlType())) {
										
										startActivity(new Intent(getApplicationContext(), LunTanHomePageActivity.class));
										
									}
								}
							});
						}
					 
						
						carouselView.setImageBitmaps(bmps);
					}
				})
				.errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						
					}
				})
				.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void getAllGroup() {
		
		final String []ids=new String[]{"1","5","6","7","8","9","10","11"};
		final ViewGroup vgs[]=new ViewGroup[]{shuiZu,chongWu,diaoJuGuan,jingGuanYuanYi,yunDongXiuXian,jiaJuBaiJian,huaHuiYuanYi,huWaiYuanYi};
		for (int i = 0; i < 8; i++) {
			final int ii=i;
			MyJsonRequest<FiveImageBean>request=new MyJsonRequest.Builder<FiveImageBean>()
					.apiVer("100")
					.typeKey("BY_Product_queryByGroup")
					.param("group_id", "15")
					.param("cate_id", ids[i])
					.requestListener(new XRequestListener<FiveImageBean>() {
						
						@Override
						public void onResponse(FiveImageBean arg0) {
							 
							list=arg0.getList();
							
							if (arg0.getCount()>0) {
								ImageView imageView=(ImageView) vgs[ii].findViewById(R.id.leftBig); 
								XImageLoader.load(list.get(0).getImg(), imageView);
//								LayoutParams params = (LayoutParams) imageView.getLayoutParams();  
//							    params.height=getWindowManager().getDefaultDisplay().getWidth()/2;  
//							    params.width =getWindowManager().getDefaultDisplay().getWidth()/2;  
//							    imageView.setLayoutParams(params);  
							}
							if (arg0.getCount()>1) {
								ImageView imageView=(ImageView) vgs[ii].findViewById(R.id.rightFirst); 
								XImageLoader.load(list.get(1).getImg(), imageView);
//								LayoutParams params = (LayoutParams) imageView.getLayoutParams();  
//							    params.height=getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    params.width =getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    imageView.setLayoutParams(params);  
							}
							if (arg0.getCount()>2) {
								ImageView imageView=(ImageView) vgs[ii].findViewById(R.id.rightSecond); 
								XImageLoader.load(list.get(2).getImg(), imageView);
//								LayoutParams params = (LayoutParams) imageView.getLayoutParams();  
//							    params.height=getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    params.width =getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    imageView.setLayoutParams(params);  
							}
							if (arg0.getCount()>3) {
								ImageView imageView=(ImageView) vgs[ii].findViewById(R.id.rightThree); 
								XImageLoader.load(list.get(3).getImg(), imageView);
//								LayoutParams params = (LayoutParams) imageView.getLayoutParams();  
//							    params.height=getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    params.width =getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    imageView.setLayoutParams(params);  
							}
							if (arg0.getCount()>4) {
								ImageView imageView=(ImageView) vgs[ii].findViewById(R.id.rightFour); 
								XImageLoader.load(list.get(4).getImg(), imageView);
//								LayoutParams params = (LayoutParams) imageView.getLayoutParams();  
//							    params.height=getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    params.width =getWindowManager().getDefaultDisplay().getWidth()/4;  
//							    imageView.setLayoutParams(params);  
							}
							
							addFenGuanListener(vgs[ii],arg0 );
							try {
								
								closeProgressDialog();
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
					})
					.errorListener(new XErrorListener() {
						
						@Override
						public void onErrorResponse(Exception exception, int code, String msg) {
try {
								
								closeProgressDialog();
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
					})
					.build();
			HttpRequest.getDefaultRequestQueue().add(request);
		}
		
		
		
	}


	private void addFenGuanListener(ViewGroup vg, final FiveImageBean arg0 ) {
		   
		    
		    View []views=new View[]{ vg.findViewById(R.id.leftBig)
		    		,vg .findViewById(R.id.rightFirst)
		    		,vg.findViewById(R.id.rightSecond)
		    		,vg.findViewById(R.id.rightThree)
		    		,vg.findViewById(R.id.rightFour)
		    		
		    		};
			for (int i=0;i<views.length;i++) {
				final int ii=i;
				views[i].setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						try {
							String id=arg0.getList().get(ii).getId();
							Intent intent=new Intent(getApplicationContext(), GoodDetailActivity.class);
							intent.putExtra("what", id);
						//	intent.putExtra("guan", ids);
							intent.putExtra("imgUrl", arg0.getList().get(ii).getImg());
							startActivity(intent);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
		 
		}
	}


	private void addClickListenrForBanner(View banner) {
		banner.findViewById(R.id.shuizu).setOnClickListener(this);
		banner.findViewById(R.id.chongwu).setOnClickListener(this);
		banner.findViewById(R.id.diaoju).setOnClickListener(this);
		banner.findViewById(R.id.jingguan).setOnClickListener(this);
		
		banner.findViewById(R.id.yundong).setOnClickListener(this);
		banner.findViewById(R.id.jiaju).setOnClickListener(this);
		banner.findViewById(R.id.huahui).setOnClickListener(this);
		banner.findViewById(R.id.huwai).setOnClickListener(this);
		
		
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
//		Intent intent=new Intent(getApplicationContext(), ShopActivity.class);
		switch (v.getId()) {
		case R.id.shuizu:
			startActivity(new Intent(getApplicationContext(), ShuiZuGuanActivity.class));
			break;

		case R.id.chongwu:
		//	intent.putExtra("what", 2);	
			startActivity(new Intent(getApplicationContext(), ChongWuGuanActivity.class));
			break;
	
		case R.id.diaoju:
		//	intent.putExtra("what",3);
			startActivity(new Intent(getApplicationContext(), DiaoJuGuanActivity.class));
			break;
	
		case R.id.jingguan:
		//	intent.putExtra("what", 4);
			startActivity(new Intent(getApplicationContext(), JingGuanYuanYiGuanActivity.class));
			break;
	
		case R.id.yundong:
		//	intent.putExtra("what", 5);
			startActivity(new Intent(getApplicationContext(), YunDongXiuXianGuanActivity.class));
			break;
		case R.id.jiaju:
		//	intent.putExtra("what", 6);
			startActivity(new Intent(getApplicationContext(), JiaJuBaiJianGuanActivity.class));
			break;
	
		case R.id.huahui:
		//	intent.putExtra("what", 7);
			startActivity(new Intent(getApplicationContext(), HuaHuiYuanYiGuanActivity.class));
			break;
		case R.id.huwai:
		//	intent.putExtra("what", 8);
			startActivity(new Intent(getApplicationContext(), HuWaiJiaJuGuanActivity.class));
			break;
		case R.id.announce:
			startActivity(new Intent(getApplicationContext(), NewsHotReadMainFragmentActivity.class));
			break;
		}
		
	} 
}
